package com.gitee.wsl.struct.router

/*
import java.io.UnsupportedEncodingException
import java.net.URI
import java.net.URLDecoder
import java.net.URLEncoder
import java.util.LinkedList
import java.util.stream.Collectors
*/

/**
 * User: lanwen
 */
/*
enum class UriPart(val splitter: String, val joiner: String) {
    SCHEME(
        UrlExt.BY_CHAR_SPLITTER,
        UrlExt.BY_CHAR_SPLITTER),
    HOST(
        UrlExt.HOST_SPLITTER,
        UrlExt.HOST_JOINER
    ),
    PORT(
        UrlExt.BY_CHAR_SPLITTER,
        UrlExt.BY_CHAR_SPLITTER
    ),
    PATH(String.format(UrlExt.WITH_DELIMITER, UrlExt.PATH_SPLITTER),
        UrlExt.BY_CHAR_SPLITTER
    ),
    QUERY(
        UrlExt.QUERY_SPLITTER,
        UrlExt.QUERY_SPLITTER
    ),
    FRAGMENT(
        UrlExt.BY_CHAR_SPLITTER,
        UrlExt.BY_CHAR_SPLITTER);
}

object UrlExt{

    const val WITH_DELIMITER = "((?<=%1\$s)|(?=%1\$s))"
    const val BY_CHAR_SPLITTER = ""
    const val HOST_SPLITTER = "\\."
    const val PATH_SPLITTER = "/"
    const val QUERY_SPLITTER = "&"
    const val HOST_JOINER = "."
    const val QUERY_NAME_VALUE_SEPARATOR = "="

    fun splitScheme(uri: URI): List<String> {
        return splitBy(UriPart.SCHEME.splitter, uri.scheme)
    }

    fun splitHost(uri: URI): List<String> {
        return splitBy(UriPart.HOST.splitter, uri.host)
    }


    fun splitPort(uri: URI): List<String> {
        return splitBy(UriPart.PORT.splitter, uri.port.toString().replace("-1", ""))
    }

    fun splitPath(uri: URI): List<String> {
        return splitBy(UriPart.PATH.splitter, uri.path)
    }

    fun splitQuery(uri: URI): List<String> {
        val queries = splitBy(UriPart.QUERY.splitter, uri.rawQuery).toMutableList()
        for (i in queries.indices) {
            queries[i] = decode(queries[i])
        }
        //sort(queries)
        return queries
    }


    fun splitFragment(uri: URI): List<String> {
        return splitBy(UriPart.FRAGMENT.splitter, uri.fragment)
    }


    fun splitBy(regex: String, what: String): List<String> {
        return what.trim().split(regex).filter { it.isNotEmpty() }
    }

    fun queryParams(uri: URI): Map<String, List<String>> {
        val params: MutableMap<String, MutableList<String>> = HashMap()
        val splitted = splitQuery(uri)
        for (pair in splitted) {
            val name: String = pair.substringBefore( QUERY_NAME_VALUE_SEPARATOR)
            val value: String = pair.substringAfter( QUERY_NAME_VALUE_SEPARATOR)
            if (params[name] != null) {
                params[name]!!.add(value)
            } else {
                val paramList: MutableList<String> = LinkedList()
                paramList.add(value)
                params[name] = paramList
            }
        }
        return params
    }

    fun removeValues(
        map: MutableMap<String, List<String>>,
        name: String,
        values: List<String>
    ) {
        map.computeIfPresent(
            name
        ) { key: String, list: List<String> ->
            list.stream()
                .filter { value: String ->
                    !values.contains(
                        value
                    )
                }
                .collect(Collectors.toList())
        }
    }


    fun decode(s: String): String {
        return try {
            URLDecoder.decode(s, "UTF-8")
        } catch (e: UnsupportedEncodingException) {
            throw java.lang.IllegalArgumentException("UTF-8 encoding is not supported")
        }
    }

    fun encode(s: String): String {
        return try {
            URLEncoder.encode(s, "UTF-8")
        } catch (e: UnsupportedEncodingException) {
            throw java.lang.IllegalArgumentException("UTF-8 encoding is not supported")
        }
    }
}*/
